package org.setamv.springcloud.feign.integrated;

import org.setamv.springcloud.entity.User;
import org.setamv.springcloud.feign.contract.CustomizedHttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/feign")
public class IntegratedFeignController {

    /**
     * 这里是feing的client
     */
    @Autowired
    private SpringCloudFeignClient feignClient;

    @RequestMapping(value = "/greetings/from/{yourName}", method = RequestMethod.GET)
    public String greetings(@PathVariable String yourName) {
        String greetings = feignClient.greetings(yourName);
        return greetings;
    }

    /**
     * 自定义注解。要使得该注解生效，必须启用 {@linkplain org.setamv.springcloud.feign.contract.ExSpringMVCContract} 类作为feign的Contract
     */
    @RequestMapping(value = "/greetings/from", method = RequestMethod.GET)
    public String grettingsByCustomizedContract() {
        String greetings = feignClient.grettingsByCustomizedContract();
        return greetings;
    }

    @RequestMapping(value = "/user/echo/by/json", method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public User echoUserByJson(@RequestBody User user) {
        User echoUser = feignClient.echoUserByJson(user);
        return echoUser;
    }

    @RequestMapping(value = "/user/echo/by/xml", method = RequestMethod.POST,
            produces = MediaType.APPLICATION_XML_VALUE, consumes = MediaType.APPLICATION_XML_VALUE)
    public User echoUserByXml(@RequestBody User user) {
        User echoUser = feignClient.echoUserByXml(user);
        return echoUser;
    }
}
